package com.test.nnooolwdeaer.activity_fragment_wooden.info_wooden;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.annotation.Nullable;

import com.test.nnooolwdeaer.R;
import com.test.nnooolwdeaer.activity_fragment_wooden.base_wooden.BaseInfoActivity_wooden;
import com.test.nnooolwdeaer.constant_wooden.Constant_wooden;
import com.test.nnooolwdeaer.dialog_wooden.PhotoUploadDialog;
import com.test.nnooolwdeaer.util_wooden.ThreadUtil_wooden;
import com.test.nnooolwdeaer.util_wooden.UriUtil;
import com.test.nnooolwdeaer.util_wooden.http_wooden.HttpManager_wooden;
import com.test.nnooolwdeaer.util_wooden.imageUtil_wooden.ImageConvert_wooden;
import com.test.nnooolwdeaer.view_wooden.widget_wooden.TitleView_wooden;

import java.io.File;
import java.io.IOException;

public class FaceUploadActivity2_Viet extends BaseInfoActivity_wooden {

    private RelativeLayout image1Layout,image2Layout;
    private ImageView image1Iv,image2Iv;

    private Bitmap image1Bitmap,image2Bitmap;
    private String image1Url,image2Url;

    private boolean imageFlag = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_face_upload_2_viet);
        init();
    }

    private void init(){
        TitleView_wooden titleView_wooden = findViewById(R.id.activity_face_upload_2_view_title);
        setBackBtn_wooden(titleView_wooden.getBackIb_wooden());

        image1Layout = findViewById(R.id.activity_face_upload_2_layout_image_1);
        image2Layout = findViewById(R.id.activity_face_upload_2_layout_image_2);
        image1Iv = findViewById(R.id.activity_face_upload_2_iv_image_1);
        image2Iv = findViewById(R.id.activity_face_upload_2_iv_image_2);

        image1Layout.setOnClickListener(v -> {
            HttpManager_wooden.getInstance_wooden().saveUserBuriedPoint("FACE_UPLOAD_ACTIVITY_2_IMAGE_1_UPLOAD_CLICK");
            imageFlag = true;
            new PhotoUploadDialog(this).show();
        });

        image2Layout.setOnClickListener(v -> {
            HttpManager_wooden.getInstance_wooden().saveUserBuriedPoint("FACE_UPLOAD_ACTIVITY_2_IMAGE_2_UPLOAD_CLICK");
            imageFlag = false;
            new PhotoUploadDialog(this).show();
        });

        findViewById(R.id.activity_face_upload_2_btn_next).setOnClickListener(v -> {
            HttpManager_wooden.getInstance_wooden().saveUserBuriedPointAndUpload("FACE_UPLOAD_ACTIVITY_2_NEXT_BTN_CLICK");
            if (TextUtils.isEmpty(image1Url)||TextUtils.isEmpty(image2Url)){
                showToast_wooden(getString(R.string.word_input_tip));
                return;
            }
            HttpManager_wooden.getInstance_wooden().saveUserBuriedPointAndUpload(image1Url,image2Url);
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case PhotoUploadDialog.Start_Camera:
                onPhotoGet();
                break;
            case PhotoUploadDialog.Open_Album:
                if (data==null) return;
                photoUri = data.getData();
                onPhotoGet();
                break;
        }
    }

    private void onPhotoGet() {
        if (photoUri == null) {
            if (imageFlag) image1Url = null;
            else image2Url = null;
            showToast_wooden(getString(R.string.photo_upload_tip));
            return;
        }
        try {
            if (imageFlag) image1Bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), photoUri);
            else image2Bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), photoUri);
            String covertPath = UriUtil.getFilePathFromUri(this, photoUri);
            if (covertPath != null) {
                ImageConvert_wooden.startConvertImage_wooden(
                        new File(covertPath),
                        (localPath, thumpImg) -> ThreadUtil_wooden.getInstance_wooden().runOnChildThread_wooden(
                                () -> HttpManager_wooden.getInstance_wooden().uploadImage(localPath)
                        )
                );
            } else {
                if (imageFlag) image1Url = null;
                else image2Url = null;
                showToast_wooden(getString(R.string.photo_upload_tip));
            }
        } catch (IOException e) {
            if (Constant_wooden.Debug) e.printStackTrace();
        }
    }

    public void onPhotoUploadResponse(String imageUrl){
        if (TextUtils.isEmpty(imageUrl)||(imageFlag&&image1Bitmap==null)||(!imageFlag&&image2Bitmap==null)){
            showToast_wooden(getString(R.string.photo_upload_tip));
            return;
        }
        if (imageFlag){
            image1Url = imageUrl;
            image1Layout.setBackground(new BitmapDrawable(getResources(), image1Bitmap));
            image1Iv.setVisibility(View.GONE);
        }else {
            image2Url = imageUrl;
            image2Layout.setBackground(new BitmapDrawable(getResources(), image2Bitmap));
            image2Iv.setVisibility(View.GONE);
        }
    }

}